package com.eboss2.sbs.adapter;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.eboss2.sbs.R;
import com.eboss2.sbs.model.TransInfo;
import com.tool.utils.utils.StringUtils;

import java.util.List;

public class AdapterTrans extends BaseAdapter {

    @SuppressWarnings("unused")
    private Context mContext;
    private LayoutInflater mInflater;
    private List<TransInfo> list;


    public AdapterTrans(Context context, final List<TransInfo> list) {
        this.mContext = context;
        this.list = list;
        mInflater = LayoutInflater.from(context);
    }

    

    @Override
    public int getCount() {
        return list == null ? 0 : list.size();
    }

    @Override
    public Object getItem(int position) {
        return list == null ? null : list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return list == null ? 0 : position;
    }

    @SuppressLint("InflateParams")
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = mInflater.inflate(R.layout.listitem_trans, null);
            holder.tvValue = convertView.findViewById(R.id.tv_value);
            holder.tvName = convertView.findViewById(R.id.tv_name);
            holder.tvMore = convertView.findViewById(R.id.tv_more);
            holder.ll_view_line = convertView.findViewById(R.id.ll_view_line);
            holder.foot_line = convertView.findViewById(R.id.foot_line);

            //设置item高度
            DisplayMetrics dm = new DisplayMetrics();
            ((Activity) mContext).getWindowManager().getDefaultDisplay().getMetrics(dm);
            int height = dm.widthPixels;// dm.heightPixels - ScreenUtils.dp2px(100, mContext);//高度
            AbsListView.LayoutParams param = new AbsListView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, height/5);
            convertView.setLayoutParams(param);

            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        final TransInfo vo = list.get(position);
        
        holder.tvName.setText(vo.getName());
        if ("1".equals(vo.getType())) {
            holder.tvValue.setText(StringUtils.formatStrMoney(vo.getValue()));
        }else{
            holder.tvValue.setText(vo.getValue());
        }

        if ((position+1) % 3 == 0){
            holder.ll_view_line.setVisibility(View.GONE);
        }else{
            holder.ll_view_line.setVisibility(View.VISIBLE);
        }

        //去除最后一组底部分隔线
        int blanceNum = list.size()%3;
        if (blanceNum != 0) {
            if (position+1 > list.size() - blanceNum) {
                holder.foot_line.setVisibility(View.GONE);
            } else {
                holder.foot_line.setVisibility(View.VISIBLE);
            }
        }else{
            if (position >= list.size() - 3){
                holder.foot_line.setVisibility(View.GONE);
            }
        }

        //最后一个
        if (position == list.size() - 1 ){
            holder.tvName.setVisibility(View.GONE);
            holder.tvValue.setVisibility(View.GONE);
            holder.tvMore.setVisibility(View.VISIBLE);


//            holder.tvMore.setText(vo.getValue());
        }else{
            holder.tvName.setVisibility(View.VISIBLE);
            holder.tvValue.setVisibility(View.VISIBLE);
            holder.tvMore.setVisibility(View.GONE);

        }

        return convertView;
    }

   

    

    private static final class ViewHolder {
        TextView tvValue;
        TextView tvName;
        ImageView tvMore;
        LinearLayout ll_view_line;
        View foot_line;
    }

}
